package com.code.generator.cn.util.fb;

import com.code.generator.cn.entity.CoderField;
import com.code.generator.cn.entity.CoderTable;
import com.code.generator.cn.util.MyFileUtil;
import com.code.generator.cn.util.PathUtil;
import com.code.generator.cn.util.base.CommonUtil;
import com.code.generator.cn.util.base.MyStringChangeUtil;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.apache.commons.lang.StringUtils;

import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FbVoUtil {
    /**
     * 生成请求查询数据
     *
     * @param coderTable
     * @param coderFields
     * @param configuration
     * @param projPath
     */
    public static void createRespDto(CoderTable coderTable, List<CoderField> coderFields, Configuration configuration, String projPath) throws IOException, TemplateException {
        Writer file = null;

        String className = coderTable.getModifyName();
        Template temp = configuration.getTemplate("/fb/queryDto.ftl");

        //获取包名
        String packageName  = PathUtil.getCreateBasePath(coderTable);


        String groupPath = packageName.replace(".", "/");
        //生成entity的路径
        String mapperPath = projPath + "/api/src/main/java/" + groupPath + "/dto/"+StringUtils.uncapitalize(coderTable.getModifyName())+"/query/";
        //设置生成的路径
        MyFileUtil.createFileDir(mapperPath);
        //设置生成的属性
        Map<String, Object> createMap = new HashMap<>();

        //设置实体对象要生成的包名
        createMap.put("packageName", packageName);
        //设置实体对象的类名
        createMap.put("className", className);

        createMap.put("classRemark",coderTable.getRemark());

        CommonUtil.createFbBaseInfo(coderTable, createMap);

        List<Map<String, Object>> fieldList = new ArrayList<>();
        for (CoderField  coderField : coderFields) {
            if (coderField.getIsSearch() == 1) {
                if(coderField.getSearchWay()!=7&&coderField.getSearchWay()!=8) {
                    Map<String, Object> fieldMap = CommonUtil.createFieldMap(coderField, createMap);
                    fieldList.add(fieldMap);
                }else{
                    String fileName=coderField.getClassFieldName();
                    String remark=coderField.getFieldRemark();
                    Map<String, Object> fieldMap = CommonUtil.createFieldMap(coderField, createMap);
                    String upName= (String) fieldMap.get("upName");

                    Map<String,Object> startMap=new HashMap<>();
                    startMap.put("name","start"+upName);
                    startMap.put("remark","查询开始"+remark);
                    startMap.put("type",MyStringChangeUtil.changeFileld(coderField.getFieldType()));
                    startMap.put("upName","Start"+upName);
                    fieldList.add(startMap);

                    Map<String,Object> endMap=new HashMap<>();
                    endMap.put("name","end"+upName);
                    endMap.put("remark","查询结束"+remark);
                    endMap.put("type",MyStringChangeUtil.changeFileld(coderField.getFieldType()));
                    endMap.put("upName","End"+upName);
                    fieldList.add(endMap);

                }
            }
        }
        if(!createMap.containsKey("DATEIMPORT")){
            createMap.put("DATEIMPORT","");
        }
        createMap.put("fieldList", fieldList);
        //设置生成的文件名
        file = new FileWriter(mapperPath + className + "QueryDto.java");
        temp.process(createMap, file);
        file.close();

    }


    /**
     * 生成响应数
     *
     * @param coderTable
     * @param coderFields
     * @param configuration
     * @param projPath
     */
    public static void createReqVo(CoderTable coderTable, List<CoderField> coderFields, Configuration configuration, String projPath) throws IOException, TemplateException {
        Writer file = null;

        String className = coderTable.getModifyName();
        Template temp = configuration.getTemplate("/fb/vo.ftl");

        //获取包名
        String packageName  = PathUtil.getCreateBasePath(coderTable);



        String groupPath = packageName.replace(".", "/");
        //生成entity的路径
        String mapperPath = projPath + "/api/src/main/java/" + groupPath + "/vo/"+StringUtils.uncapitalize(coderTable.getModifyName())+"/";
        //设置生成的路径
        MyFileUtil.createFileDir(mapperPath);
        //设置生成的属性
        Map<String, Object> createMap = new HashMap<>();
        //设置实体对象要生成的包名
        //设置实体对象要生成的包名
        createMap.put("packageName", packageName);
        //设置实体对象的类名
        createMap.put("className", className);

        createMap.put("classRemark",coderTable.getRemark());

        CommonUtil.createFbBaseInfo(coderTable, createMap);

        List<Map<String, Object>> fieldList = new ArrayList<>();
        for (CoderField coderField : coderFields) {
            Map<String, Object> fieldMap = CommonUtil.createFieldMap(coderField,createMap);

            fieldList.add(fieldMap);
        }
        if(!createMap.containsKey("DATEIMPORT")){
            createMap.put("DATEIMPORT","");
        }
        createMap.put("fieldList", fieldList);
        //设置生成的文件名
        file = new FileWriter(mapperPath + className + "Vo.java");
        temp.process(createMap, file);
        file.close();

    }

    public static void createInputVo(CoderTable coderTable, List<CoderField> coderFields, Configuration configuration, String projPath) throws IOException, TemplateException {
        Writer file = null;
        String className = coderTable.getModifyName();
        Template temp = configuration.getTemplate("/fb/inputDto.ftl");

        //获取包名
        String packageName  = PathUtil.getCreateBasePath(coderTable);



        String groupPath = packageName.replace(".", "/");
        //生成entity的路径
        String mapperPath = projPath + "/api/src/main/java/" + groupPath + "/dto/"+StringUtils.uncapitalize(coderTable.getModifyName())+"/input/";
        //设置生成的路径
        MyFileUtil.createFileDir(mapperPath);
        //设置生成的属性
        Map<String, Object> createMap = new HashMap<>();
        //设置实体对象要生成的包名
        createMap.put("packageName", packageName);
        //设置实体对象的类名
        createMap.put("className", className);

        createMap.put("classRemark",coderTable.getRemark());

        CommonUtil.createFbBaseInfo(coderTable, createMap);
        List<Map<String, Object>> fieldList = new ArrayList<>();
        for (CoderField coderField : coderFields) {
            if (coderField.getIsForm() == 1) {
                Map<String, Object> fieldMap = CommonUtil.createFieldMap(coderField,createMap);
                if (coderField.getIsNull() != null && coderField.getIsNull() == 1) {
                    fieldMap.put("require", ",required =true");
                } else {
                    fieldMap.put("require", "");
                }

                if (coderField.getIsForm() != null && coderField.getIsForm() == 0) {
                    fieldMap.put("hidden", ",hidden = true");
                } else {
                    fieldMap.put("hidden", "");
                }



                fieldList.add(fieldMap);
            }
        }
        if(!createMap.containsKey("DATEIMPORT")){
            createMap.put("DATEIMPORT","");
        }
        createMap.put("fieldList", fieldList);
        //设置生成的文件名
        file = new FileWriter(mapperPath + className + "InputDto.java");
        temp.process(createMap, file);
        file.close();
    }
}
